﻿using UnityEngine;
using UnityEngine.EventSystems;
using System.Collections;
using System;

namespace ugui.demo.dragline
{
    public class DragLine : MonoBehaviour,IBeginDragHandler,IDragHandler,IEndDragHandler
    {
        public Canvas _canvas;
        public RectTransform leftTrans;
        public RectTransform moddleTrans;
        public RectTransform rightTrans;
        public bool isPointerExit = false;
        
        public void OnBeginDrag(PointerEventData eventData)
        {
            leftTrans.gameObject.SetActive(true);
            moddleTrans.gameObject.SetActive(false);
            rightTrans.gameObject.SetActive(false);

            Vector2 localPosition;
            RectTransformUtility.ScreenPointToLocalPointInRectangle(transform.GetComponent<RectTransform>(), eventData.position, _canvas.worldCamera, out localPosition);
            Debug.LogError("OnBeginDrag localPosition:" + localPosition);
            leftTrans.localPosition = new Vector3(localPosition.x, 0,0);


            moddleTrans.anchorMax = new Vector2(0, 1);
            moddleTrans.anchorMin = new Vector2(0, 0);
            moddleTrans.pivot = new Vector2(0, 0);
            moddleTrans.offsetMin = new Vector2(localPosition.x, 0);
            moddleTrans.offsetMax = new Vector2(localPosition.x, 0);
            moddleTrans.anchoredPosition = new Vector2(localPosition.x, 0);


        }

        public void OnDrag(PointerEventData eventData)
        {

            moddleTrans.gameObject.SetActive(true);
            Vector2 localPosition;
            RectTransformUtility.ScreenPointToLocalPointInRectangle(transform.GetComponent<RectTransform>(), eventData.position, _canvas.worldCamera, out localPosition);
            Debug.LogError("OnDrag localPosition:" + localPosition);
            if (localPosition.x >= leftTrans.localPosition.x)
            {
                moddleTrans.anchorMax = new Vector2(0, 1);
                moddleTrans.anchorMin = new Vector2(0, 0);
                moddleTrans.pivot = new Vector2(0, 0);
                moddleTrans.offsetMin = new Vector2(leftTrans.localPosition.x, 0);
                moddleTrans.offsetMax = new Vector2(localPosition.x > 710 ? 710 : localPosition.x, 0);

                moddleTrans.anchoredPosition = new Vector2(leftTrans.localPosition.x, 0);
            }
            else
            {

                moddleTrans.anchorMax = new Vector2(1, 1);
                moddleTrans.anchorMin = new Vector2(1, 0);
                moddleTrans.pivot = new Vector2(1, 1);
                moddleTrans.offsetMin = new Vector2((localPosition.x - leftTrans.localPosition.x)< -leftTrans.localPosition.x? -leftTrans.localPosition.x: (localPosition.x - leftTrans.localPosition.x), 0);
                moddleTrans.offsetMax = new Vector2(0, 0);

                moddleTrans.anchoredPosition = new Vector2(-710 + leftTrans.localPosition.x, 0);
            }
        }

        public void OnEndDrag(PointerEventData eventData)
        {
            rightTrans.gameObject.SetActive(true);
            Vector2 localPosition;
            RectTransformUtility.ScreenPointToLocalPointInRectangle(transform.GetComponent<RectTransform>(), eventData.position, _canvas.worldCamera, out localPosition);
            Debug.LogError("OnEndDrag localPosition:" + localPosition);
            
            if (localPosition.x >= leftTrans.localPosition.x)
            {
                moddleTrans.anchorMax = new Vector2(0, 1);
                moddleTrans.anchorMin = new Vector2(0, 0);
                moddleTrans.pivot = new Vector2(0, 0);
                moddleTrans.offsetMin = new Vector2(leftTrans.localPosition.x, 0);
                moddleTrans.offsetMax = new Vector2(localPosition.x > 710 ? 710 : localPosition.x, 0);

                moddleTrans.anchoredPosition = new Vector2(leftTrans.localPosition.x, 0);
                rightTrans.localPosition = new Vector3(localPosition.x > 710 ? 710 : localPosition.x, 0, 0);
            }
            else
            {

                moddleTrans.anchorMax = new Vector2(1, 1);
                moddleTrans.anchorMin = new Vector2(1, 0);
                moddleTrans.pivot = new Vector2(1, 1);
                moddleTrans.offsetMin = new Vector2((localPosition.x - leftTrans.localPosition.x) < -leftTrans.localPosition.x ? -leftTrans.localPosition.x : (localPosition.x - leftTrans.localPosition.x), 0);
                moddleTrans.offsetMax = new Vector2(0, 0);

                moddleTrans.anchoredPosition = new Vector2(-710 + leftTrans.localPosition.x, 0);
                rightTrans.localPosition = new Vector3(localPosition.x <=0  ? 0 : localPosition.x, 0, 0);
            }
        }
        

       
    }
}
